package org.miyuki4j.cfgtool;

import lombok.Getter;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/8/7/星期四 17:39
 * @Version 1.0
 */
public class FieldNameValidator {

    // Java 关键字集合（包含保留字）
    public static final Set<String> JAVA_KEYWORDS = new HashSet<>(Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while", "true", "false", "null", "get", "set", "is"));

    public static ValidationResult validate(String fieldType, String fieldName) {
        final ValidationResult result = new ValidationResult(fieldName);

        // 1. 检查是否为空
        if (fieldName == null || (fieldName = fieldName.trim()).isEmpty()) {
            result.addError("字段名不能为空");
            return result;
        }

        // 2. 检查首字符
        char firstChar = fieldName.charAt(0);
        if (!(Character.isJavaIdentifierStart(firstChar) || firstChar == '_' || firstChar == '$')) {
            result.addError("首字符必须是字母、下划线(_)或美元符号($)");
        }

        // 3. 检查后续字符
        for (int i = 1; i < fieldName.length(); i++) {
            char c = fieldName.charAt(i);
            if (!Character.isJavaIdentifierPart(c) && c != '$') {
                result.addError("包含非法字符: '" + c + "'");
                break;
            }
        }

        // 4. 检查是否为关键字
        if (JAVA_KEYWORDS.contains(fieldName.toLowerCase())) {
            result.addError("不能使用Java关键字");
        }

        // 5. 检查长度（可选）
        if (fieldName.length() > 64) {
            result.addError("字段名过长（建议不超过64字符）");
        }

        // 6.bool字段不能以is开头
        if ("bool".equalsIgnoreCase(fieldType)) {
            if (fieldName.toLowerCase().startsWith("is")) {
                result.addError("bool字段不能以is开头");
            }
        }

        return result;
    }

    // 验证结果封装类
    public static class ValidationResult {
        @Getter
        private final String fieldName;
        @Getter
        private final StringBuilder errors = new StringBuilder();
        @Getter
        private boolean isValid = true;

        public ValidationResult(String fieldName) {
            this.fieldName = fieldName;
        }

        public void addError(String error) {
            isValid = false;
            if (errors.length() > 0) {
                errors.append(" | ");
            }
            errors.append(error);
        }

        @Override
        public String toString() {
            if (isValid) {
                return String.format("[✓] 字段%s -> 合法", fieldName);
            } else {
                return String.format("[✗] %s -> 非法: %s", fieldName, errors);
            }
        }
    }
}
